package com.bw.message

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.base.constants.RoutePath
import com.bw.base.constants.UserConst
import com.bw.common.entity.CustomEvent
import com.bw.common.view.BaseMvvmFragment
import com.bw.message.adapter.MessageAdapter
import com.bw.message.databinding.FragmentMessageBinding
import com.bw.message.net.MessageViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

@Route(path = RoutePath.PATH_MESSAGE_FRAGMENT)
@AndroidEntryPoint
class MessageFragment : BaseMvvmFragment<FragmentMessageBinding, MessageViewModel>() {

    private lateinit var mAdapter: MessageAdapter

    override fun initView() {

        binding.topBar.cardTx.visibility = View.VISIBLE
        binding.topBar.imgBell.visibility = View.VISIBLE

        mAdapter = MessageAdapter()
        binding.rv.run {
            setHasFixedSize(true)
            setItemViewCacheSize(10)
//            setRecycledViewPool(RecyclerView.RecycledViewPool().apply {
//                setMaxRecycledViews(0,8)
//                setMaxRecycledViews(1,8)
//                setMaxRecycledViews(2,8)
//                clear()
//            })
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
        }



    }

    override fun initData() {
        vm.messageSuccess.observe(this@MessageFragment.requireActivity()){
            mAdapter.data.addAll(it)
            mAdapter.notifyDataSetChanged()
            EventBus.getDefault().post(CustomEvent("MainActivity",it.size,2))
        }

    }

    override fun onResume() {
        super.onResume()
        if(mAdapter.data.isEmpty()){
            val uid = MMKV.defaultMMKV().decodeInt(UserConst.USER_ID)
            vm.getMessage(155,uid)
        }
    }

    override fun handEvent(event: CustomEvent) {
        super.handEvent(event)
        Log.i("--s", "handEvent: SASS")
    }






}